﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Web;
using NHibernate;
using NHibernate.Linq;
using DD.Mappings;

namespace DD.NHibernateProvider
{
    public class NHibernateLinqDataSourceView : LinqDataSourceView
    {
        NHibernateLinqDataSource owner;

        public NHibernateLinqDataSourceView(NHibernateLinqDataSource owner, HttpContext context)
            : base(owner, "DefaultView", context)
        {
            this.owner = owner;
        }

        protected override MemberInfo GetTableMemberInfo(Type contextType)
        {
            if (typeof(GenericNHibernateContext).IsAssignableFrom(contextType))
                return GetNHibernateTableMemberInfo(contextType);
            else
                // return base.GetTableMemberInfo(contextType);
                throw new Exception("PASHA1");
        }


        public override void Delete(System.Collections.IDictionary keys, System.Collections.IDictionary oldValues, System.Web.UI.DataSourceViewOperationCallback callback)
        {
            if (!this.CanDelete)
                throw new NotSupportedException("CanDelete = false");

            ISession session = NHibernateFactory.Current.OpenSession();
            //session.Linq<Book>()

            var r = from b in session.Linq<Book>()
                    select b.Name;

            this.owner.Delete(keys, oldValues);

            //(ISession session = NHibernateFactory.Current.OpenSession())
            //{
                
            //}


            //base.Delete(keys, oldValues, callback);
        }

        private MemberInfo GetNHibernateTableMemberInfo(Type contextType)
        {
            return new StubFieldInfo(this.TableName);
        }

        class StubFieldInfo : FieldInfo
        {
            private string name;

            public StubFieldInfo(string name)
            {
                this.name = name;
            }

            public override object GetValue(object obj)
            {
                return ((GenericNHibernateContext)obj).GetTable(name);
            }

            #region Not implemented (not needed)

            public override FieldAttributes Attributes
            {
                get { throw new NotImplementedException(); }
            }

            public override RuntimeFieldHandle FieldHandle
            {
                get { throw new NotImplementedException(); }
            }

            public override Type FieldType
            {
                get { throw new NotImplementedException(); }
            }

            public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            public override Type DeclaringType
            {
                get { throw new NotImplementedException(); }
            }

            public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                throw new NotImplementedException();
            }

            public override object[] GetCustomAttributes(bool inherit)
            {
                throw new NotImplementedException();
            }

            public override bool IsDefined(Type attributeType, bool inherit)
            {
                throw new NotImplementedException();
            }

            public override string Name
            {
                get { throw new NotImplementedException(); }
            }

            public override Type ReflectedType
            {
                get { throw new NotImplementedException(); }
            }

            #endregion
        }
    }
}
